//
//  TypeProModel.m
//  LiWuShuo
//
//  Created by 袁小航 on 14/11/17.
//  Copyright (c) 2014年 lanou3g.com 蓝鸥科技. All rights reserved.
//

#import "TypeProModel.h"
#import "AFAppDotNetAPIClient.h"

@implementation TypeProModel

-(instancetype)initWithProAttributes:(NSDictionary *)attributes
{
    self = [super init];
    if (!self) {
        return nil;
    }
    
    self.typeImageUrl = [attributes objectForKey:@"cover_image_url"];
    self.typeInfo = [attributes objectForKey:@"title"];
    self.typeCollectNum = [attributes objectForKey:@"likes_count"];
    self.typeUrl = [attributes objectForKey:@"content_url"];
    
    return self;
}

+ (NSURLSessionDataTask *)typeProductWithBlock:(void (^)(NSArray *, NSError *))block subId:(NSInteger)sId
{
    return [[AFAppDotNetAPIClient sharedClient] GET:[NSString stringWithFormat:@"http://api.liwushuo.com/v1/collections/%d/posts?limit=12&offset=0", sId] parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *postsFromResponse = [[responseObject objectForKey:@"data"] objectForKey:@"posts"];
        
        NSMutableArray *mutablePosts = [NSMutableArray array];
        for (NSDictionary *dic in postsFromResponse) {
            TypeProModel *model = [[TypeProModel alloc]initWithProAttributes:dic];
            [mutablePosts addObject:model];
        }
        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (block) {
            block([NSArray array], nil);
        }
    }];
}

+ (NSURLSessionDataTask *)typePHProductWithBlock:(void (^)(NSArray *, NSError *))block
                                            subId:(NSInteger)sId
                                            page:(NSInteger)page
{
    return [[AFAppDotNetAPIClient sharedClient] GET:[NSString stringWithFormat:@"http://api.liwushuo.com/v1/channels/%d/items?limit=12&offset=%d", sId, page] parameters:nil success:^(NSURLSessionDataTask *task, id responseObject) {
        NSArray *postsFromResponse = [[responseObject objectForKey:@"data"] objectForKey:@"items"];
        
        NSMutableArray *mutablePosts = [NSMutableArray array];
        for (NSDictionary *dic in postsFromResponse) {
            TypeProModel *model = [[TypeProModel alloc]initWithProAttributes:dic];
            [mutablePosts addObject:model];
        }
        if (block) {
            block([NSArray arrayWithArray:mutablePosts], nil);
        }
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        if (block) {
            block([NSArray array], nil);
        }
    }];
}

@end
